using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SIOGDE.AccesoDatos.Entidades;
using SIOGDE.Negocios;
using SIOGDE.Negocios.Servicios;
using Didsoft.Mvc.DataModel;
using Didsoft.Mvc.Controller;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace SIOGDE.Web.Controllers
{
    public class MediaController : BaseController<Media>
    {
	    #region interface servicios Media

		private IMediaNegociosServicio servicio = BusinessFactory.Current.GetMediaNegocios();

		#endregion
		
		
		#region constructor and overridable

        public MediaController()
        {		    
            base.SetDataModelService(servicio);            
        }
		        

		

		#endregion


		#region	actions

        [HttpPost]
        public JsonResult JSONPagedListByFilterLite(string filter = "", int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            try
            {
                var searchedItems =  servicio.BuscarPorFiltroLite(filter);
                var items = searchedItems.Skip(jtStartIndex).Take(jtPageSize).ToList();
                return Json(new { Result = "OK", Records = items, TotalRecordCount = searchedItems.Count() });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }



		[HttpGet]
		public override ActionResult Index()
        {
            return RedirectToAction("FilterList");
        }

        public FileContentResult Imagen(string guid)
        {
            var media = servicio.ObtenerPorGuid(guid);
            if (media != null)
                return new FileContentResult(media.Recurso, "image/png");
            else
                return new FileContentResult(new byte[0], "image/png");
        }

		
        public ActionResult Cargar(string guid, string titulo)
        {
            var media = servicio.ObtenerPorGuid(guid);
            if (media == null)
                return View(new Media() { FechaCreacion = DateTime.Now, TituloRecurso = titulo, GuidSolicitante = guid });
            
            return View(media);
        }

      

        [HttpPost]
        public ActionResult Cargar(string guid, string titulo, HttpPostedFileBase archivo, string redireccion = "")
        {
            try
            {
                var media = servicio.ObtenerPorGuid(guid);
                /*MemoryStream memoryStream = new MemoryStream();
                archivo.InputStream.CopyTo(memoryStream);
                var bytes = memoryStream.ToArray();
                memoryStream.Dispose();*/

                if (media == null)
                    media = new Media()
                    {
                        GuidSolicitante = guid,
                        TituloRecurso = titulo,
                        FechaCreacion = DateTime.Now
                    };

                media.Recurso =  Didsoft.Utils.Media.ImageResize.ResizeStream(200, archivo.InputStream);
                servicio.Guardar(media);

                if (!string.IsNullOrEmpty(redireccion))
                    return Redirect(redireccion);


                return View(media);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("DataFormError", ex.Message);
                return View(new Media() { FechaCreacion = DateTime.Now, TituloRecurso = titulo, GuidSolicitante = guid });                
            }           
        }


        #endregion

    }
}

